package com.wx.app.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.administrator.newemptyapplication.R;
import com.wx.app.mylib.utils.ToastHelper;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FilterActivity2 extends Activity {

    private boolean isClassify;

    private String[] group_title_array = new String[]{"颈椎测试", "腰部测试", "第三代测试"};
    // 子视图显示文字
    private String[][] child_text_array = new String[][]{
            {"是否经常感到左臂疼痛？", "是否经常熬夜？", "您的踝关节有刺痛的现象吗？", "是否经常用凉水洗头？"},
            {"人民日报？", "塔什干6月22日？", "记者 杜尚泽？", "两国元首？"},
            {"取得的丰硕成果？", "一致决定建立中乌全面战略？", "推动两国合作向前？", "国家主席习近平？"},
            {"记者马占成摄？", "央视网消息？", "特别是通过简政放权？", "推行“两随机、一公开”？"}};


    private HashMap<String, String> map = new HashMap<>();
    private HashMap<String, Boolean> stateMap = new HashMap<>();
    @BindView(R.id.tv_filter2_title)
    TextView title;
    @BindView(R.id.epLv_filter)
    ExpandableListView epListView;
    private String dataArrStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter2);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String titleName = intent.getStringExtra("titleName");//.........
        isClassify = intent.getBooleanExtra("isClassify", false);//...........
        dataArrStr = intent.getStringExtra("arrayStr");
        JSONArray dataArr = JSONObject.parseArray(dataArrStr);
        title.setText(titleName);
        initData(dataArr);
        initView();
    }

    private int mGroupPosition = 0;
    private boolean isFirst = true;

    private void initView() {
        assert epListView != null;
        final MyBaseExpandableListAdapter expandableListAdapter = new MyBaseExpandableListAdapter();
        epListView.setAdapter(expandableListAdapter);
        epListView.setGroupIndicator(null);
        epListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                if (isClassify) {  //如果是true就是单选
                    if (isFirst) {
                        isFirst = false;
                        mGroupPosition = groupPosition;
                    } else if (mGroupPosition != groupPosition) {
                        mGroupPosition = groupPosition;
                        stateMap.clear();
                        map.clear();
                        expandableListAdapter.notifyDataSetChanged();
                    }
                }
                TextView tvChildTitle = (TextView) v.findViewById(R.id.tv_title_child);
                ImageView ivChecked = (ImageView) v.findViewById(R.id.iv_child_checked);
                String childTitle = child_text_array[groupPosition][childPosition];
                Toast.makeText(FilterActivity2.this, "点击" + childTitle, Toast.LENGTH_SHORT).show();
                if (!stateMap.containsKey(groupPosition + "_" + childPosition) || !(stateMap.get(groupPosition + "_" + childPosition))) {
                    stateMap.put(groupPosition + "_" + childPosition, true);
                    map.put(groupPosition + "_" + childPosition, tvChildTitle.getText().toString());
                    ivChecked.setVisibility(View.VISIBLE);
                    tvChildTitle.setTextColor(Color.parseColor("#2CBCAD"));
                } else {
                    stateMap.put(groupPosition + "_" + childPosition, false);
                    map.remove(groupPosition + "_" + childPosition);
                    ivChecked.setVisibility(View.INVISIBLE);
                    tvChildTitle.setTextColor(Color.parseColor("#535353"));
                }
                return true;
            }
        });
    }

    private void initData(JSONArray dataArr) {


    }

    public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {

        private ImageView ivState;
        private TextView groupTitle;
        // 一级标签上的logo图片数据源
        // 一级标签上的标题数据源
        // 一级标签上的状态图片数据源

        @Override
        public int getGroupCount() {
            return group_title_array.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return child_text_array[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return group_title_array[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return child_text_array[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.group_layout, null);
            groupTitle = (TextView) convertView.findViewById(R.id.group_title);
            ivState = (ImageView) convertView.findViewById(R.id.group_state);
            groupTitle.setText(group_title_array[groupPosition]);
            if (isExpanded) {
                ivState.setImageResource(R.drawable.group_up);
            } else {
                ivState.setImageResource(R.drawable.group_down);
            }
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
            ChildHolder childHolder = null;
            if (convertView == null) {
                childHolder = new ChildHolder();
                convertView = getLayoutInflater().inflate(R.layout.group_child_layout, null);
                childHolder.childTitle = (TextView) convertView.findViewById(R.id.tv_title_child);
                childHolder.ivCheck = (ImageView) convertView.findViewById(R.id.iv_child_checked);
                convertView.setTag(childHolder);
            } else {
                childHolder = (ChildHolder) convertView.getTag();
            }
            if (!stateMap.containsKey(groupPosition + "_" + childPosition) || !(stateMap.get(groupPosition + "_" + childPosition))) {
                childHolder.childTitle.setTextColor(Color.parseColor("#535353"));
                childHolder.ivCheck.setVisibility(View.INVISIBLE);
            } else {
                childHolder.childTitle.setTextColor(Color.parseColor("#2CBCAD"));
                childHolder.ivCheck.setVisibility(View.VISIBLE);
            }
            String childTitleStr = child_text_array[groupPosition][childPosition];
            childHolder.childTitle.setText(childTitleStr);
            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

    public class ChildHolder {
        TextView childTitle;
        ImageView ivCheck;
    }

    StringBuffer buffer = new StringBuffer();

    @OnClick(R.id.btn_filter2_confirm)
    void confirm() {
        buffer.delete(0, buffer.length());
        for (String str : map.values()) {
            buffer.append(str);
        }
        ToastHelper.showToast(FilterActivity2.this, buffer.toString());
    }

    @OnClick(R.id.ll_filter2_back)
    void back() {
        finish();
        overridePendingTransition(0, R.anim.base_slide_right_out);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.base_slide_right_out);
    }
}
